// pages/product/components/categories/ca.js
const WXAPI = require('apifm-wxapi')
const AUTH = require('../../../../utils/auth')
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    categoryList: [], //分类列表
    catId: null, //当前分类
  },
  lifetimes: {
    attached() {
      console.log(this.getList)
      this.getList()
    },
  },
  /**
   * 组件的方法列表
   */

  methods: {
    getList() {
      console.log(app.globalData.shopNo);
      WXAPI.shopGetProductCategory(app.globalData.shopNo)
        .then(res => {
          const categoryList = res && res.data || []
          Array.isArray(categoryList) && this.setData({
            categoryList
          })
          const defaultCatNo = categoryList && categoryList[0] && categoryList[0].catNo || ''
          this.changeCategory(defaultCatNo)
        })
        .catch(err => {
          console.log(err)
        })
    },
    
    changeCategory(catId) {
      this.setData({
        catId
      })
      this.triggerEvent('changeCategroy', {
        catId
      })
    },
    switchCategory(e) {
      console.log(e)
      const catno = e && e.target && e.target.dataset && e.target.dataset.catno || ''
      console.log(catno)
      catno && this.changeCategory(catno)
    }
  }
})